<?php
namespace App\Service\Order;

use Carbon\Carbon;

use App\Models\Bill\Bill;

use App\Models\Bill\BillPay;

use App\Models\Bill\BillGoods;

use App\Models\Bill\BillTrace;

abstract class OrderService
{

    protected $bill; //订单数据
    
    protected $bill_no; //订单号

    protected $datetime; //下单时间

    protected $creator; //创建人
    
    protected $pcustID; //会员号
    
    protected $custID; //会员名
    
    protected $openId; //微信粉丝openId
    
    protected $cardNo; //会员卡号

    protected $pay_status; //支付状态

    protected $insert = []; //待保存的数据

    protected function __construct($bill, $creator)
    {
        $this->datetime = Carbon::now();
        $this->bill_no = generate_seqno('seqno_bill', null, true);
        $this->bill = $bill;
        $this->creator = $creator;
        $this->pcustID = $bill['pcustID'];
        $this->custID = $bill['custID'];
        $this->cardNo = $bill['cardNo'];
        $this->openId = $bill['openId'];
        //支付状态
        $this->pay_status = 0;
        $this->bill['payed_time'] = null;
        if( $this->bill['bill_payable'] == 0 ){  //说明订单已经无需支付（优惠券、积分、余额等抵扣）
            $this->bill['payed_time'] = Carbon::now();
            $this->pay_status = 1;
        } else if ( $this->bill['bill_points_money'] > 0 || $this->bill['bill_balance_money'] > 0 ) {
            $this->pay_status = 2;
        }

        if (!isset($this->bill['invoiceID'])) {
            $this->bill['invoiceID'] = 0;
        }

        if (!isset($this->bill['invoice_type'])) {
            $this->bill['invoice_type'] = '';
        }

        if (!isset($this->bill['invoice_content_id'])) {
            $this->bill['invoice_content_id'] = 0;
        }

        if (!isset($this->bill['invoice_title_type'])) {
            $this->bill['invoice_title_type'] = '';
        }

        if (!isset($this->bill['invoice_title'])) {
            $this->bill['invoice_title'] = '';
        }

        if (!isset($this->bill['invoice_content'])) {
            $this->bill['invoice_content'] = '';
        }

        if (!isset($this->bill['invoice_amount'])) {
            $this->bill['invoice_amount'] = '';
        }

        if (!isset($this->bill['bill_stock_type'])) {
            $this->bill['bill_stock_type'] = 1;
        }

        if (!isset($this->bill['split_type'])) {
            $this->bill['split_type'] = 1;
        }

        if (!isset($this->bill['send_deliver_type'])) {
            $this->bill['send_deliver_type'] = 0;
        }

        if (!isset($this->bill['bill_cross_border'])) {
            $this->bill['bill_cross_border'] = '';
        }

        if (!isset($this->bill['added_value_tax'])) {
            $this->bill['added_value_tax'] = '';
        }

        if (!isset($this->bill['consumption_tax'])) {
            $this->bill['consumption_tax'] = '';
        }

        if (!isset($this->bill['identification_card'])) {
            $this->bill['identification_card'] = '';
        }

        if (!isset($this->bill['identification_name'])) {
            $this->bill['identification_name'] = '';
        }

        if (!isset($this->bill['tuan_status'])) {
            $this->bill['tuan_status'] = 0;
        }

        if (!isset($this->bill['parent_bill_no'])) {
            $this->bill['parent_bill_no'] = 0;
        }
    }

    /**
     * 创建订单主表数据
     * @return bool
     */
    protected function createMaster()
    {

        $bill = new Bill();
        $bill->uuid = makeUuid();
        $bill->timeStamp = $this->datetime;
        $bill->creator = $this->creator;
        $bill->createTime = $this->datetime;
        $bill->bill_no = $this->bill_no;
        $bill->pcustID = $this->bill['pcustID'];
        $bill->custID = $this->bill['custID'];
        $bill->bill_status = 0;
        $bill->bill_return_status = 0;
        $bill->bill_refund_status = 0;
        $bill->bill_logistics_status = 0;
        $bill->bill_totalMoney = $this->bill['bill_totalMoney'];
        $bill->bill_totalPaid = $this->bill['bill_totalPaid'];
        $bill->bill_tariff_money = $this->bill['bill_tariff_money'];
        $bill->bill_totalPoints = $this->bill['bill_totalPoints'];
        $bill->bill_pointsPrice = $this->bill['bill_pointsPrice'];
        $bill->bill_subtract_money = $this->bill['bill_subtract_money'];
        $bill->bill_points_money = $this->bill['bill_points_money'];
        $bill->bill_balance_money = $this->bill['bill_balance_money'];
        $bill->bill_coupon_money = $this->bill['bill_coupon_money'];
        $bill->bill_card_money = 0;
        $bill->bill_payable = $this->bill['bill_payable'];
        $bill->bill_total_amount = $this->bill['bill_total_amount'];
        $bill->bill_withdraw_amount = 0;
        $bill->deliverid = $this->bill['deliverid'];
        $bill->deliver_name = $this->bill['deliver_name'];
        $bill->deliver_province = $this->bill['deliver_province'];
        $bill->deliver_provinceid = $this->bill['deliver_provinceid'];
        $bill->deliver_city = $this->bill['deliver_city'];
        $bill->deliver_cityid = $this->bill['deliver_cityid'];
        $bill->deliver_county = $this->bill['deliver_county'];
        $bill->deliver_countyid = $this->bill['deliver_countyid'];
        $bill->deliver_address = $this->bill['deliver_address'];
        $bill->deliver_mobile = $this->bill['deliver_mobile'];
        $bill->deliver_phone = $this->bill['deliver_phone'];
        $bill->sendTypeID = $this->bill['sendTypeID'];
        $bill->sendTypeName = $this->bill['sendTypeName'];
        $bill->ziti = $this->bill['ziti'];
        $bill->send_region_id = $this->bill['send_region_id'];
        $bill->send_region_name = $this->bill['send_region_name'];
        $bill->deliver_fee = $this->bill['deliver_fee'];
        $bill->deliver_feePaid = $this->bill['deliver_feePaid'];
        $bill->pay_type = $this->bill['pay_type'];
        $bill->payTypeID = $this->bill['payTypeID'];
        $bill->payTypeName = $this->bill['payTypeName'];
        $bill->pay_status = $this->pay_status;
        $bill->payed_time = $this->bill['payed_time'];
        $bill->pay_id_str = $this->bill['pay_id_str'];
        $bill->goods_name_str = $this->bill['goods_name_str'];
        $bill->billPlatform = $this->bill['billPlatform'];
        $bill->billChannel = $this->bill['billChannel'];
        $bill->billType = $this->bill['billType'];
        $bill->invoice = $this->bill['invoice'];
        $bill->invoiceID = $this->bill['invoiceID'];
        $bill->invoice_type = $this->bill['invoice_type'];
        $bill->invoice_title_type = $this->bill['invoice_title_type'];
        $bill->invoice_title = $this->bill['invoice_title'];
        $bill->invoice_content_id = $this->bill['invoice_content_id'];
        $bill->invoice_content = $this->bill['invoice_content'];
        $bill->invoice_amount = $this->bill['invoice_amount'];
        $bill->package_no = null;
        $bill->bill_stock_type = $this->bill['bill_stock_type'];
        $bill->split_type = $this->bill['split_type'];
        $bill->parent_bill_no = $this->bill['parent_bill_no'];
        $bill->mallID = $this->bill['mallID'];
        $bill->mallCode = $this->bill['mallCode'];
        $bill->mallName = $this->bill['mallName'];
        $bill->barcode = null;
        $bill->activity_id = $this->bill['activity_id'];
        $bill->remark = $this->bill['remark'];
        $bill->is_comment = 0;
        $bill->is_enter_erp = 0;
        $bill->express_number = null;
        $bill->express_company = null;
        $bill->pickup_start_time = $this->bill['pickup_start_time'];
        $bill->pickup_end_time = $this->bill['pickup_end_time'];
        $bill->send_time = $this->bill['send_time'];
        $bill->bill_cancel_reason = null;
        $bill->send_deliver_type = $this->bill['send_deliver_type'];
        $bill->verify_time = null;
        $bill->print_num = 0;
        $bill->supplier_id = $this->bill['supplier_id'];
        $bill->zt_id = $this->bill['zt_id'];
        $bill->zt_name = $this->bill['zt_name'];
        $bill->zt_code = $this->bill['zt_code'];
        $bill->bill_cross_border = $this->bill['bill_cross_border'];
        $bill->added_value_tax = $this->bill['added_value_tax'];
        $bill->consumption_tax = $this->bill['consumption_tax'];
        $bill->identification_card = $this->bill['identification_card'];
        $bill->identification_name = $this->bill['identification_name'];
        $bill->tuan_status = $this->bill['tuan_status'];

        $this->insert['bill_master'][] = $bill;

        return true;

    }


    /**
     * 创建订单商品信息
     * @return bool
     */
    protected function createGoods()
    {

        $goods_name = [];

        foreach( $this->bill['goods'] as &$goods ) {

            if ( !isset($goods['recordPrice']) ) {
                $goods['recordPrice'] = 0;
            }
            if ( !isset($goods['goods_pointsPrice']) ) {
                $goods['goods_pointsPrice'] = 0;
            }

            $goods_name[] = $goods['goodsName'];

            $bill_goods = new BillGoods();
            $bill_goods->uuid = makeUuid();
            $bill_goods->timeStamp = $this->datetime;
            $bill_goods->createTime = $this->datetime;
            $bill_goods->creator = $this->creator;
            $bill_goods->bill_no = $this->bill_no;
            $bill_goods->bill_goods_no = generate_seqno('seqno_bill_goods', null, true);
            $bill_goods->goods_goodsID = $goods['goodsID'];
            $bill_goods->goods_postID = $goods['postID'];
            $bill_goods->product_code = $goods['product_code'];
            $bill_goods->goods_name = $goods['goodsName'];
            $bill_goods->goods_spec = $goods['spec'];
            $bill_goods->goods_amount = $goods['goods_amount'];
            $bill_goods->goods_storePrice = $goods['salePrice'];
            $bill_goods->goods_purchasePrice = $goods['price'];
            $bill_goods->goods_marketPrice = $goods['marketPrice'];
            $bill_goods->goods_pointsPrice = $goods['goods_pointsPrice'];
            $bill_goods->goods_points = $goods['salePoints'];
            $bill_goods->goods_weight = $goods['goodsWeight'];
            $bill_goods->goods_from = $goods['goods_from'];
            $bill_goods->goods_groupID = $goods['goods_groupID'];
            $bill_goods->goods_status = 1;
            $bill_goods->goods_time = null;
            $bill_goods->goods_delivercount = 0;
            $bill_goods->goods_remark = $goods['goods_remark'];
            $bill_goods->comment_flg = 0;
            $bill_goods->goods_withdrawAmount = 0;
            $bill_goods->goods_rejectionAmount = 0;
            $bill_goods->virtual_id = $goods['virtual_id'];
            $bill_goods->recordPrice = $goods['recordPrice'];
            $this->insert['bill_goods'][] = $bill_goods;
        }

        $this->bill['goods_name_str'] = implode(',', $goods_name);

        return true;

    }


    /**
     * 创建订单支付信息
     * @return bool
     */
    protected function createPay()
    {

        $pay_id = [];
        $pay_name = [];

        foreach( $this->bill['pay'] as &$pay ){

            if ( !isset($pay['deduct_amount']) ) {
                $pay['deduct_amount'] = 0;
            }
            if ( !isset($pay['pay_code']) ) {
                $pay['pay_code'] = null;
            }
            if ( !isset($pay['pay_time']) ) {
                $pay['pay_time'] = null;
            }
            if ( !isset($pay['paid_confirm_status']) ) {
                $pay['paid_confirm_status'] = 0;
            }

            $bill_pay = new BillPay();
            $bill_pay->uuid = makeUuid();
            $bill_pay->timeStamp = $this->datetime;
            $bill_pay->createTime = $this->datetime;
            $bill_pay->creator = $this->creator;
            $bill_pay->bill_no = $this->bill_no;
            $bill_pay->payTypeID = $pay['payTypeID'];
            $bill_pay->payTypeName = $pay['payTypeName'];
            $bill_pay->type = $pay['type'];
            $bill_pay->pay_amount = $pay['pay_amount'];
            $bill_pay->deduct_amount = $pay['deduct_amount'];
            $bill_pay->pay_code = $pay['pay_code'];
            $bill_pay->pay_time = $pay['pay_time'];
            $bill_pay->paid_confirm_status = $pay['paid_confirm_status'];
            $bill_pay->paid_confirmer = null;
            $bill_pay->paid_confirm_time = null;
            $this->insert['bill_pay'][] = $bill_pay;
            $pay_id[] = $pay['payTypeID'];
            $pay_name[] = $pay['payTypeName'];

        }

        $this->bill['pay_id_str'] = implode(',', $pay_id);
        $this->bill['pay_name_str'] = implode(',', $pay_name);

        return true;

    }

    /**
     * 创建订单轨迹信息
     * @return bool
     */
    protected function createTrace()
    {

        $bill_trace = new BillTrace();
        $bill_trace->uuid = makeUuid();
        $bill_trace->timeStamp = $this->datetime;
        $bill_trace->createTime = $this->datetime;
        $bill_trace->creator = $this->creator;
        $bill_trace->bill_no = $this->bill_no;
        $bill_trace->bill_status = 0;
        $bill_trace->content = '您的订单已提交，请等待系统确认';
        $this->insert['bill_trace'][] = $bill_trace;

        return true;

    }

    abstract function submit();


}
